package com.wangshuo.webadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangshuo.webadmin.entity.*;
import com.wangshuo.webadmin.mapper.*;
import com.wangshuo.webadmin.service.*;
import com.wangshuo.webadmin.vo.attr.AttrValueVo;
import com.wangshuo.webadmin.vo.graph.GraphVo;
import com.wangshuo.webadmin.vo.room.RoomDetailVo;
import com.wangshuo.webadmin.vo.room.RoomItemVo;
import com.wangshuo.webadmin.vo.room.RoomQueryVo;
import com.wangshuo.webadmin.vo.room.RoomSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author 13326
* @description 针对表【room_info(房间信息表)】的数据库操作Service实现
* @createDate 2025-01-21 16:43:50
*/
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
    implements RoomInfoService{
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

@Autowired
private RoomLeaseTermService roomLeaseTermService;
@Autowired
private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
@Autowired
private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    public void saveOrupdateroom(RoomSubmitVo roomSubmitVo){
boolean isupdate=roomSubmitVo.getId()!=null;
super.saveOrUpdate(roomSubmitVo);
if(isupdate){
    LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(GraphInfo::getItemId,roomSubmitVo.getId());
    lambdaQueryWrapper.eq(GraphInfo::getItemType,2);
    graphInfoService.remove(lambdaQueryWrapper);

    LambdaQueryWrapper<RoomAttrValue> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
    lambdaQueryWrapper1.eq(RoomAttrValue::getRoomId,roomSubmitVo.getId());
    roomAttrValueService.remove(lambdaQueryWrapper1);

    LambdaQueryWrapper<RoomFacility> lambdaQueryWrapper2=new LambdaQueryWrapper<>();
    lambdaQueryWrapper2.eq(RoomFacility::getRoomId,roomSubmitVo.getId());
    roomFacilityService.remove(lambdaQueryWrapper2);

    LambdaQueryWrapper<RoomLabel> labelLambdaQueryWrapper3=new LambdaQueryWrapper<>();
    labelLambdaQueryWrapper3.eq(RoomLabel::getRoomId,roomSubmitVo.getId());
    roomLabelService.remove(labelLambdaQueryWrapper3);

    LambdaQueryWrapper<RoomPaymentType> lambdaQueryWrapper3=new LambdaQueryWrapper<>();
    lambdaQueryWrapper3.eq(RoomPaymentType::getRoomId,roomSubmitVo.getId());
    roomPaymentTypeService.remove(lambdaQueryWrapper3);

    LambdaQueryWrapper<RoomLeaseTerm> lambdaQueryWrapper4=new LambdaQueryWrapper<>();
    lambdaQueryWrapper4.eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId());
    roomLeaseTermService.remove(lambdaQueryWrapper4);

}
        List<GraphVo> graphVoList=roomSubmitVo.getGraphVoList();
if(!CollectionUtils.isEmpty(graphVoList)){
    ArrayList<GraphInfo> list=new ArrayList<>();
    for (GraphVo graphVo:graphVoList){
        GraphInfo graphInfo=new GraphInfo();
        graphInfo.setItemType(2);
        graphInfo.setItemId(roomSubmitVo.getId());
        graphInfo.setName(graphVo.getName());
        graphInfo.setUrl(graphVo.getUrl());
        list.add(graphInfo);
    }
    graphInfoService.saveBatch(list);
}
List<Long> list2=roomSubmitVo.getAttrValueIds();
if(!CollectionUtils.isEmpty(list2)){
    List<RoomAttrValue> roomAttrValueList=new ArrayList<>();
    for (Long id:list2){
   RoomAttrValue roomAttrValue=new RoomAttrValue();
   roomAttrValue.setRoomId(roomSubmitVo.getId());
   roomAttrValue.setAttrValueId(id);
   roomAttrValueList.add(roomAttrValue);
     }
    roomAttrValueService.saveBatch(roomAttrValueList);
}
List<Long> list3=roomSubmitVo.getFacilityInfoIds();
if(!CollectionUtils.isEmpty(list3)){
    List<RoomFacility> roomFacilityList=new ArrayList<>();
    for (Long id:list3){
        RoomFacility roomFacility=new RoomFacility();
        roomFacility.setRoomId(roomSubmitVo.getId());
        roomFacility.setFacilityId(id);
        roomFacilityList.add(roomFacility);
    }
    roomFacilityService.saveBatch(roomFacilityList);

}
List<Long> labelInfoId=roomSubmitVo.getLabelInfoIds();
if(!CollectionUtils.isEmpty(labelInfoId)){
    ArrayList<RoomLabel> roomLabelArrayList=new ArrayList<>();
    for (Long id:labelInfoId){
        RoomLabel roomLabel=new RoomLabel();
        roomLabel.setRoomId(roomSubmitVo.getId());
        roomLabel.setLabelId(id);
        roomLabelArrayList.add(roomLabel);
    }
    roomLabelService.saveBatch(roomLabelArrayList);
}
List<Long> list4=roomSubmitVo.getPaymentTypeIds();
if(!CollectionUtils.isEmpty(list4)){
    ArrayList<RoomPaymentType> roomPaymentTypeArrayList=new ArrayList<>();
    for (Long id:list4){
        RoomPaymentType roomPaymentType=new RoomPaymentType();
        roomPaymentType.setRoomId(roomSubmitVo.getId());
        roomPaymentType.setPaymentTypeId(id);
        roomPaymentTypeArrayList.add(roomPaymentType);
    }
    roomPaymentTypeService.saveBatch(roomPaymentTypeArrayList);
}
List<Long> list5=roomSubmitVo.getLeaseTermIds();
if(!CollectionUtils.isEmpty(list5)){
    ArrayList<RoomLeaseTerm> roomLeaseTermsArraylist=new ArrayList<>();
    for (Long id:list5){
        RoomLeaseTerm roomLeaseTerm=new RoomLeaseTerm();
        roomLeaseTerm.setRoomId(roomSubmitVo.getId());
        roomLeaseTerm.setLeaseTermId(id);
        roomLeaseTermsArraylist.add(roomLeaseTerm);
    }
    roomLeaseTermService.saveBatch(roomLeaseTermsArraylist);
}
    }
    @Override
    public IPage<RoomItemVo> pageApartmernt(IPage<RoomItemVo> page, RoomQueryVo queryVo){
return roomInfoMapper.pageApartmernt(page,queryVo);

    }
    @Override
    public RoomDetailVo getdetail(Long id){
        RoomInfo roomInfo=roomInfoMapper.selectById(id);
        RoomDetailVo roomDetailVo=new RoomDetailVo();
        ApartmentInfo apartmentInfo=apartmentInfoMapper.selectinfoById(id);
        List<GraphVo> list=graphInfoMapper.selectlistbyid(2,id);
        List<AttrValueVo> list1=attrValueMapper.selectlistbyid(id);
        List<FacilityInfo> list2=facilityInfoMapper.selectlistbyid1(id);
        List<LabelInfo> list3=labelInfoMapper.selectlistbyid1(id);
        List<PaymentType> list4=paymentTypeMapper.selectlistbyid(id);
        List<LeaseTerm> list5=leaseTermMapper.selectlistbyid(id);


        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setGraphVoList(list);
        roomDetailVo.setAttrValueVoList(list1);
        roomDetailVo.setFacilityInfoList(list2);
        roomDetailVo.setLabelInfoList(list3);
        roomDetailVo.setPaymentTypeList(list4);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setLeaseTermList(list5);


        return roomDetailVo;
    }
    @Override
    public void removeByid(Long id){
super.removeById(id);
LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(GraphInfo::getItemId,id);
lambdaQueryWrapper.eq(GraphInfo::getItemType,2);
graphInfoService.remove(lambdaQueryWrapper);

LambdaQueryWrapper<RoomAttrValue> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
lambdaQueryWrapper1.eq(RoomAttrValue::getRoomId,id);
roomAttrValueService.remove(lambdaQueryWrapper1);

LambdaQueryWrapper<RoomFacility> lambdaQueryWrapper2=new LambdaQueryWrapper<>();
lambdaQueryWrapper2.eq(RoomFacility::getRoomId,id);
roomFacilityService.remove(lambdaQueryWrapper2);

LambdaQueryWrapper<RoomLabel> labelLambdaQueryWrapper=new LambdaQueryWrapper<>();
labelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
roomLabelService.remove(labelLambdaQueryWrapper);

LambdaQueryWrapper<RoomPaymentType> lambdaQueryWrapper3=new LambdaQueryWrapper<>();
lambdaQueryWrapper3.eq(RoomPaymentType::getRoomId,id);
roomPaymentTypeService.remove(lambdaQueryWrapper3);

LambdaQueryWrapper<RoomLeaseTerm> lambdaQueryWrapper4=new LambdaQueryWrapper<>();
lambdaQueryWrapper4.eq(RoomLeaseTerm::getRoomId,id);
roomLeaseTermService.remove(lambdaQueryWrapper4);


    }
}




